package ModelLayer;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Iterator;

public class SaleItemRecords {

	private ArrayList<SaleItem> saleItems;
	//singleton stuff
	private static SaleItemRecords instance;
	
	private SaleItemRecords() {
		saleItems = new ArrayList<SaleItem>();
	}
	
	public static SaleItemRecords getInstance() {
		if (instance == null) {
			instance = new SaleItemRecords();
		}
		return instance;
	}
	
	public void createSaleItem(String name, int location, double bPrice, double sPrice, double tax,
			String um, int minStock, int maxStock, int currStock, int discountQ, int contractorID) throws RealException {
		SaleItem saleItem = new SaleItem(name, location, bPrice, sPrice, tax, um, minStock,
				maxStock, currStock, discountQ, contractorID);
		saleItems.add(saleItem);
		if (!saleItems.contains(saleItem)) {
			throw new RealException("Something went wrong during creation of product!");
		}
	}
	
	public SaleItem getSaleItem(String barcode) throws RealException {
		SaleItem theSaleItem = null;
		Iterator<SaleItem> it = saleItems.iterator();
		boolean found = false;
		while (it.hasNext() && !found) {
			SaleItem saleItem = it.next();
			if (saleItem.getBarcode().equals(barcode)) {
				theSaleItem = saleItem;
				found = true;
			}
		}
		if (!found) throw new RealException("Product not found!");
		return theSaleItem;
	}
	
	public void checkSaleItem(String barcode) throws RealException {
		Iterator<SaleItem> it = saleItems.iterator();
		boolean found = false;
		while (it.hasNext() && !found) {
			SaleItem saleItem = it.next();
			if (saleItem.getBarcode().equals(barcode)) {
				found = true;
			}
		}
		if (!found) throw new RealException("Product not found!");
	}
	
	public void updateSaleItem(String barcode, int fieldID, String value) throws RealException {
		if (!value.equals(null)) {
			if (fieldID >= 1 && fieldID <= 11) {
				SaleItem saleItem = getSaleItem(barcode);
				switch (fieldID) {
				case 1:
					saleItem.setName(value);
					break;
				case 2:
					saleItem.setbPrice(Double.parseDouble(value));
					break;
				case 3:
					saleItem.setsPrice(Double.parseDouble(value));
					break;
				case 4:
					saleItem.setTax(Double.parseDouble(value));
					break;
				case 5:
					saleItem.setUm(value);
					break;
				case 6:
					saleItem.setMinStock(Integer.parseInt(value));
					break;
				case 7:
					saleItem.setMaxStock(Integer.parseInt(value));
					break;
				case 8:
					saleItem.setCurrStock(Integer.parseInt(value));
					break;
				case 9:
					saleItem.setDiscountQ(Integer.parseInt(value));
					break;
				case 10:
					saleItem.setLocation(Integer.parseInt(value));
					break;
				case 11:
					saleItem.setContractor(Integer.parseInt(value));
					break;
				}
			} else {
				throw new RealException("Passed fieldID was lesser than 1, or more then 11");
			}
		} else {
			throw new RealException("Passed value is \"null\"!");
		}
	}
	
	public void deleteSaleItem(String barcode) throws RealException {
		SaleItem saleItem = getSaleItem(barcode);
		if (saleItem != null) {
			saleItems.remove(saleItem);
		}
	}
	
	public String getSaleItemInfo(String barcode) throws RealException {
		SaleItem saleItem = getSaleItem(barcode);
		return saleItem.toString();
	}
	
	public ArrayList<String> getAllSaleItemInfo() {
		ArrayList<String> saleItemsInfo = new ArrayList<String>();
		if (saleItems.size() != 0) {
			for (SaleItem saleItem : saleItems) {
				saleItemsInfo.add(saleItem.toString());
			}
		} 
		if (saleItemsInfo.size() != 0) {
			return saleItemsInfo;
		} else {
			saleItemsInfo.add("There are none!");
			return saleItemsInfo;
		}
	}
	
	public ArrayList<String> findSaleItems(String searchPattern) throws RealException {
		ArrayList<String> matchingResults = new ArrayList<String>();
		for (SaleItem saleItem : saleItems) {
			if (saleItem.getName().startsWith(searchPattern)) {
				matchingResults.add(saleItem.toString());
			}
		}
		if (!matchingResults.isEmpty()) {
			return matchingResults;
		} else {
			throw new RealException("There aren't any products matching your search.");
		}
	}
	
	public ArrayList<SaleItem> getSaleItemRecords() {
		return saleItems;
	}

	public void setSaleItemRecords(ArrayList<SaleItem> saleItems) {
		this.saleItems = saleItems;
	}
	
	public void save(ObjectOutputStream obj_out) throws IOException {
		obj_out.writeObject(saleItems);
		obj_out.writeObject(new Long(SaleItem.getLastBarcodeSeed()));
	}
	
	@SuppressWarnings("unchecked")
	public void load(ObjectInputStream obj_in) throws IOException, ClassNotFoundException {
		this.saleItems = (ArrayList<SaleItem>) obj_in.readObject();
		SaleItem.setLastBarcodeSeed(((Long)obj_in.readObject()).longValue());
	}
}
